package com.cndw.kungfu.model.goods.app;

import java.util.Map;

import com.cndw.kungfu.config.ConstantsCost;
import com.cndw.kungfu.config.GoodsEffectConfig;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.exception.ErrObjectException;
import com.cndw.kungfu.exception.ErrPopException;
import com.cndw.kungfu.ext.LangUtil;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.activity.domain.RateChest;
import com.cndw.kungfu.model.sys.ChatMain;
import com.cndw.kungfu.model.sys.LogServer;

/**
 * 开启宝箱
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2012-1-11 下午5:56:57
 */
public class AppDo103 extends AppBase {

	private StringBuffer sb = new StringBuffer();

	@Override
	public boolean check() throws Exception {
		RateChest rateChest = RateChest.getRateChest(goodsId);
		if (rateChest == null) {
			throw new ErrPopException(LanguageLib.get("comm.err.args"));
		}
		checkBlack(1);
		if (rateChest.getGold() > 0) {
			if (player.getGoldAll() < rateChest.getGold()) {
				throw new ErrObjectException(LanguageLib.get("comm.err.gold"));
			}
			player.alterGoldAll(-rateChest.getGold());
			msg.put("gold", player.getGold());
			msg.put("goldBing", player.getGoldBing());
			sb.append(LanguageLib.get("goods.use.costgold", rateChest.getGold()));
			LogServer.costEventLog(playerId, rateChest.getGold(), ConstantsCost.openChest);
		}
		return true;
	}

	@Override
	public Map<String, Object> run() throws Exception {
		RateChest rateChest = RateChest.getRateChest(goodsId);
		int finalIndex = NumberProcesser.rateIndex(rateChest.getRates(), 10000);
		if (finalIndex >= 0 && rateChest.getAwards().length > finalIndex) {
			int finalGoodsId = rateChest.getAwards()[finalIndex];
			GoodsMode awardMode = Context.getGoodsServer().getModeById(finalGoodsId);
			if (awardMode != null) {
				int finalNum = rateChest.getNums()!= null && rateChest.getNums().length > finalIndex ? rateChest.getNums()[finalIndex]:1;
				Context.getGoodsServer().giveGoodsBing(playerId, finalGoodsId, finalNum, rateChest.getBind());
				sb.append(LanguageLib.get("goods.use.getgoods", awardMode.getName(),finalNum));
	
				
				ticpString = LanguageLib.get("goods.use.ratesuc", LangUtil.makeHtml(awardMode.getColor(), awardMode.getName(),finalNum));
				if (goodsId == GoodsEffectConfig.zzGoodsId[2]) {
					ChatMain.systemMessage(LanguageLib.get("holiday.zongzi.use", player.getPlayerName(),
							LangUtil.makeHtml(awardMode.getColor(), awardMode.getName())));
				} else if (awardMode.getColor() >= 3) {
					ChatMain.systemMessage(LanguageLib.get("holiday.zongzi.useo", player.getPlayerName(),
							LangUtil.makeHtml(awardMode.getColor(), awardMode.getName()), goodsMode.getName()));
				}
			} else {
				LogUtil.getMain().error("err " + finalIndex + " | " + player.getAccountName());
			}
		} else {
			LogUtil.getMain().error("err " + finalIndex);
			return msg;
		}
		logString = sb.toString();

		return msg;
	}
}
